package com.example.controller;

import com.example.entity.QueryPermissionVo;
import com.example.entity.UserInfo;
import com.example.entity.base.BaseResult;
import com.example.entity.base.ResponseData;
import com.example.service.SakuraService;
import com.example.service.impl.OrderTaskServic;
import com.example.util.ConfigUtil;
import com.example.util.redis.RedisPlugin;
import com.example.util.redis.redisUtil.RedisUtil;
import com.google.common.util.concurrent.AbstractExecutionThreadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 控制层controller
 * @Author wangXiao
 * @Date 2021/9/6
 **/

@RestController
@RequestMapping("/test")
@Slf4j
@Api("swaggerDemoController相关的api")
public class SakuraController extends BaseController{

    @Resource(name = "sakuraServiceImpl")
    public SakuraService sakuraService;

    @Autowired
    public OrderTaskServic orderTaskServic;

    private AbstractExecutionThreadService service;

    @Autowired
    public RedisTemplate  redisTemplate;


    @PostConstruct
    public void initTest() {
        System.out.println("执行初始化");
    }
    /**
     * 获取全部用户
     *
     * @return
     */
    @PostMapping("/getAllUser")
    @ApiOperation(value = "单独一个入参测试接口", notes = "接口描述，例如：只能输入‘朦胧的夜’")
    public void detail() {

        List<String> userlist = sakuraService.selectUsers();
        log.info("开始统计用户");
        for (String name : userlist) {
            System.out.println(name);
        }
    }

    @GetMapping("/getAllUser2")
    @ApiOperation(value = "单独一个入参测试接口2", notes = "接口描述，例如：只能输入‘朦胧的夜’")
    public void getAllUser2(@RequestBody(required = false) UserInfo requestDto) {
        log.info("开始统计用户2222222");
        List<String> userlist = sakuraService.selectUsers();
        for (String name : userlist) {
            System.out.println(name);
        }

        //正在开发的代码测试

        //修复代码

    }

    @PostMapping("/testAsync")
    @ApiOperation(value = "测试异步注解", notes = "测试异步注解’")
    public void testAsync() throws InterruptedException {
        orderTaskServic.cancelOrder();
        orderTaskServic.orderTask();
    }

    @PostMapping("/testYml")
    @ApiOperation(value = "测试读取yml并执行", notes = "测试读取yml并执行’")
    public void testYml() {
        try {
            sakuraService.getReadmeConfig();
            if (service != null && service.isRunning()) {
                throw new RuntimeException("有正在运行中的程序");
            }
        } catch (IOException e) {
            System.out.println("异常");
        }
    }

    @PostMapping("/getConfigBean")
    @ApiOperation(value = "测试Bean注解", notes = "测试Bean注解’")
    public void getConfigBean() {
        String name = ConfigUtil.getConfigBeanName();
        System.out.println(name);

    }

    @PostMapping("/getTreeMenu")
    @ApiOperation(value = "获取树形菜单", notes = "获取树形菜单’")
    public List<QueryPermissionVo> getTreeMenu() {
        List<QueryPermissionVo> list = sakuraService.treeMenuList();
        return  list;
    }

    @PostMapping("/getCacheTest")
    @ApiOperation(value = "测试springCache", notes = "测试springCache’")
    public ResponseData getCacheTest(@RequestParam String name) {
        List<String> list = sakuraService.getCacheTest(name);
        return ResponseData.ok(list.get(0));
    }

    @PostMapping("/getRedis")
    @ApiOperation(value = "测试Redis", notes = "测试Redis’")
    public void getRedisTest() {
      //  RedisPlugin.use().set("wx-1", "66");
      //  RedisPlugin.useRedisUtil().set("wx-2", "777");
     /*   List<Object> list = RedisPlugin.use().lGet("list", 0, -1);
        System.out.println(RedisPlugin.use().get("key2"));*/
        RedisUtil.set("wx-3","9999");
        RedisPlugin.use().publish("wx_test","这事我的测试消息");


    }

    @PostMapping("/getdataSource1")
    @ApiOperation(value = "getdataSource1", notes = "getdataSource1’")
    public void getdataSource1() {
       sakuraService.getdataSource1();
    }

    @PostMapping("/getdataSource2")
    @ApiOperation(value = "getdataSource2", notes = "getdataSource2’")
    public void getdataSource2() {
        sakuraService.getdataSource2();
    }

    @PostMapping("/testTransactional")
    @ApiOperation(value = "testTransactional", notes = "testTransactional’")
    public BaseResult testTransactional() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("1");
        list.add("2");
        for(String str: list){
            BaseResult result =  sakuraService.testTransactional(str);
            if(result.getCode()!=200){
                return BaseResult.newError(result.getMessage());
            }
        }
        return BaseResult.newSuccess("成功");
    }
}
